package com.luyuan.crm.member.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.MemberGroup;
import com.luyuan.crm.member.service.MemberGroupService;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;

@Component("memberGroupAction")
@Scope("view")
public class MemberGroupAction extends BaseAction {
    @Resource(name = "memberGroupService")
    private MemberGroupService memberGroupService;

    // 查询条件
    private MemberGroup criteria = new MemberGroup();

    // 添加或编辑模型
    private MemberGroup memberGroup;

    public MemberGroup getCriteria() {
        return criteria;
    }

    public MemberGroup getMemberGroup() {
        if (memberGroup != null) {
            return memberGroup;
        }

        String userId = this.getRequestParameter("groupId");
        if (userId != null && !"".equals(userId)) {
            memberGroup = memberGroupService.getById(userId);
        }
        if (memberGroup == null) {
            memberGroup = new MemberGroup();
        }
        return memberGroup;
    }

    public void setCriteria(MemberGroup criteria) {
        this.criteria = criteria;
    }

    public void setMemberGroup(MemberGroup memberGroup) {
        this.memberGroup = memberGroup;
    }

    public DataModel getTableModel() {
        TablePage limit = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder order : tableOrders) {
            if (order.toOrder() != null) {
                orders.add(order.toOrder());
            }
        }
        orders.add(Order.desc("id"));
        CriteriaTemplate c = CriteriaTemplate.forClass(MemberGroup.class);
        RestrictionTemplate r = new RestrictionTemplate(true);
        c.add(r.like("groupName", criteria.getGroupName()));
        Page page = memberGroupService.pagedQuery(c, limit.getPage(), limit.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public void addMemberGroup() {
        memberGroupService.addMemberGroup(memberGroup);

        addGlobalI18nMessage("crud_add_success_msg");
    }

    public void updateMemberGroup() {
        memberGroupService.updateMemberGroup(memberGroup);

        addGlobalI18nMessage("crud_update_success_msg");
    }

    public void removeMemberGroup(MemberGroup MemberGroup) {
        memberGroupService.removeMemberGroup(MemberGroup);

        addGlobalI18nMessage("数据删除成功！");
    }
}
